package tmproject.hlhj.fhp.cartrans.modules

import android.content.Context
import com.example.mymvp.BaseModule
import com.lzy.okgo.model.Response
import tmproject.hlhj.fhp.cartrans.convert.MyException
import tmproject.hlhj.fhp.cartrans.http.Apis
import tmproject.hlhj.fhp.cartrans.http.BaseAciton
import tmproject.hlhj.fhp.cartrans.http.BaseObserver
import tmproject.hlhj.fhp.cartrans.http.LoadingAction
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.ClassListBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.utils.LoadingUtil
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils

/**
 * Created by Administrator on 2018/8/26/026.
 */
class LearnModule :BaseModule {
    var listener:OnLearnLisener?=null
    fun getClassList(context: Context,token:String?,page:Int,pageSize:Int){
        Apis.getClassList(context,token,page,pageSize)
                .doOnSubscribe(LoadingAction(context,false))
                .subscribe(object :BaseObserver<Response<ClassListBean>>(context){
                    override fun failed(e: Throwable?) {
                        listener?.classResp(null)
                    }
                    override fun httpSuccess(t: Response<ClassListBean>) {
                        listener?.classResp(t.body())
                    }

                    override fun onError(e: Throwable) {
                        LoadingUtil.hideLoading()
                        if (e is MyException){
                            if ( e.code==408){
                                listener?.noMore()
                            }
                        }else{
                            ToastUtils.toast(e?.message!!)

                        }
                        log("okgo","错误${e.toString()}")
                    }
                    override fun httpErroCode(baseBean: BaseBean) {
                    }
                })
    }
    interface OnLearnLisener{
        fun classResp(classListBean: ClassListBean?)
        fun noMore()
    }
}